using System;
using System.Collections.Generic;

namespace NExt.Core.Model.Enum
{
    public class AnonymousObject
    {
        private readonly IDictionary<Type, AnonymousObject> Dictionary;

        protected AnonymousObject()
        {
            Dictionary = new Dictionary<Type, AnonymousObject>();
            Type runtimeType = this.GetType();
            if (runtimeType != typeof(AnonymousObject))
            {
                Dictionary.Add(runtimeType, this);
            }
        }

        public ICollection<AnonymousObject> GetInnerObjects()
        {
            return Dictionary.Values;
        }

        public static AnonymousObject operator +(AnonymousObject anonymousObjectObject1, AnonymousObject anonymousObjectObject2)
        {
            AnonymousObject resultAnonymousObject = new AnonymousObject();

            foreach (KeyValuePair<Type, AnonymousObject> keyValuePair in anonymousObjectObject1.Dictionary)
            {
                Type type = keyValuePair.Key;
                AnonymousObject anonymousObject = keyValuePair.Value;
                resultAnonymousObject.Dictionary[type] = anonymousObject;
            }

            foreach (KeyValuePair<Type, AnonymousObject> keyValuePair in anonymousObjectObject2.Dictionary)
            {
                Type type = keyValuePair.Key;
                AnonymousObject anonymousObject = keyValuePair.Value;
                resultAnonymousObject.Dictionary[type] = anonymousObject;
            }

            return resultAnonymousObject;
        }

        public static AnonymousObject operator -(AnonymousObject anonymousObjectObject1, AnonymousObject anonymousObjectObject2)
        {
            AnonymousObject compositeAnonymousObject = new AnonymousObject();

            foreach (KeyValuePair<Type, AnonymousObject> keyValuePair in anonymousObjectObject1.Dictionary)
            {
                Type type = keyValuePair.Key;
                AnonymousObject anonymousObject = keyValuePair.Value;
                compositeAnonymousObject.Dictionary[type] = anonymousObject;
            }

            foreach (KeyValuePair<Type, AnonymousObject> keyValuePair in anonymousObjectObject2.Dictionary)
            {
                Type type = keyValuePair.Key;
                compositeAnonymousObject.Dictionary.Remove(type);
            }
            return compositeAnonymousObject;
        }
    }
}